/**
 * @Time: 2018/11/19 15:27
 * @Author: wangmin
 * @File: json_io
 * @Software: GoLand
 */
package jsonio

import (
	"encoding/json"
)

type Empty struct{}

// 解析客户端请求参数
func Input(data []byte, parser interface{}) (bool, string) {
	err := json.Unmarshal(data, &parser)
	if err != nil {
		return false, "请求参数错误：" + err.Error()
	}
	return true, ""
}

// 响应客户端请求
func out(code int, data interface{}, msg string) map[string]interface{} {
	if data == nil {
		data = &Empty{}
	}
	return map[string]interface{}{
		"Code": code,
		"Data": data,
		"Msg":  msg,
	}
}

// 成功时响应
func Success(data interface{}, msg string) map[string]interface{} {
	return out(0, data, msg)
}

// 失败时响应
func Fail(code int, msg string, data interface{}) map[string]interface{} {
	return out(code, data, msg)
}
